import app from '@/app'
import { get, update, del } from '@/api/erp/tax'

import base from '@/app/model'

// 继承总模型
const model = new base;

model.get = () => {
  return new Promise((resolve, jeject) => {
    get().then(res => {
      model.setList(res)
      resolve(res)
    }).catch(res => {
      jeject(res)
    })
  })
}

model.update = (...arge) => {
  return new Promise((resolve, jeject) => {
    update(...arge).then(res => {
      model.get()
      app.ui.message('success', res.message)
      resolve(res)
    }).catch(res => {
      app.ui.message('error', 'error.' + res.message)
      jeject(res)
    })
  })
}

model.del = (...arge) => {
  return new Promise((resolve, jeject) => {
    del(...arge).then(res => {
      model.get()
      app.ui.message('success', res.message)
      resolve(res)
    }).catch(res => {
      app.ui.message('error', 'error.' + res.message)
      jeject(res)
    })
  })
}

model.name = name => {
  return model.getList().find(item => item.name == name)
}

model.id = id => {
  return model.getList().find(item => item.id == id)
}

model.tax = id => {
  const tax = model.id(id)
  return tax ? Number(tax.iva) : 0
}

model.req = id => {
  const tax = model.id(id)
  return tax ? Number(tax.req) : 0
}

model.law = id => {
  const tax = model.id(id)
  return tax ? Number(tax.law) : 0
}

model.taxName = id => {
  const tax = model.id(id)
  return tax ? tax.name : ''
}


model.default = () => {
  return model.getList()[0]
}

export default model